/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import databaseinterface.DatabaseInterface;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import objects.FileInfo;
import objects.UserInfo;

/**
 *
 * @author tinvukhac
 */
public class FileServices {

    public int share(String serverIp, String userId, FileInfo file){
        try {
                int fileId;
                databaseinterface.DatabaseInterface dataitf = null;
                dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
                fileId = dataitf.insertNewFileInfo(userId, file.getFileName(),file.getFileSize(), file.getFileDescription());
                if (fileId != -1) { 
                    return fileId;
                } else {
                    JOptionPane.showMessageDialog(null, "share file not success", "Exception", 0);
                    return -1;
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.toString(), "Exception", 0);
            }
        return -1;
    }

    public List<FileInfo> getSharedFileList(String serverIp){

        List<FileInfo> list = new ArrayList<FileInfo>();

        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");

            list = dataitf.getSharedFileList();

            return list;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString(), "Exception", 0);
        }
        return list;
    }

    public List<UserInfo> getFileSeeders(int fileId, String serverIp){
        List<UserInfo> list = new ArrayList<UserInfo>();

        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");

            list = dataitf.getFileSeeders(fileId);

            return list;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString(), "Exception", 0);
        }
        return list;
    }
    
    public FileInfo getFileInfo(int Id,String serverIp){
        FileInfo file=null;
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            file = dataitf.getFileInfo(Id);
        } catch (Exception e) {
            System.out.println("getFileInfo in FileServices");
            e.printStackTrace();
        }
        return  file;
    }
    
    public List<UserInfo> getSharingUser(int fileId,String serverIp){
        List<UserInfo> sharingList=new ArrayList<UserInfo>();
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            sharingList = dataitf.getSharingUser(fileId);
        } catch (Exception e) {
            System.out.println("getSharingUser in FileServices");
            e.printStackTrace();
        }
        return sharingList;
    }
    
    public List<UserInfo> getDownloadingUser(int fileId,String serverIp){
        List<UserInfo> downloadingList=new ArrayList<UserInfo>();
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            downloadingList = dataitf.getDownloadingUser(fileId);
        } catch (Exception e) {
            System.out.println("getDownloadingUser in FileServices");
            e.printStackTrace();
        }
        return downloadingList;
    }
    
    public void confirmDownloading(int fileId,String serverIp,String userId){
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            dataitf.confirmDownloading(fileId,userId);
        } catch (Exception e) {
            System.out.println("confirmDownloading in FileServices");
            e.printStackTrace();
        }
    }
    
    public void confirmDownloadComplete(int fileId,String userId,String serverIp){
        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");
            dataitf.confirmDownloadComplete(fileId,userId);
        } catch (Exception e) {
            System.out.println("confirmDownloadComplete in FileServices");
            e.printStackTrace();
        }
    }
    
    public List<FileInfo> searchFile(String pattern, boolean isFullTextSearch, String serverIp){
        List<FileInfo> list = new ArrayList<FileInfo>();

        try {
            databaseinterface.DatabaseInterface dataitf = null;
            dataitf = (DatabaseInterface) Naming.lookup("//" + serverIp + "/data");

            list = dataitf.searchFile(pattern, isFullTextSearch);

            return list;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString(), "Exception", 0);
        }
        return list;
    }
}
